#!/bin/sh 
#
#Script name: script11.sh
#
#Author: Piyush Garg (s3218012)
#
#Purpose: This script is used for checking designed to run at 
#		user login and to analyse the output of the quota command to
#	     	determine what percentage of the quota is currently in use. 
#	  If usage is over some limit, then the script will need to 
#		print a warning reporting the current percent usage.


##########################################################################

#  Creating the funtion funtionquota() for getting the quota of all 
#  file system inside the /home directory


functionquota()		#function for getting the getting quota space
{
	LIMIT=90	#Variable for checking the disk weightage (Threshold Value)

	result=`quota -v | grep -i '^/home' | awk '{print int($2/$3*100)}'`
}
###########################################################################


###########################################################################

################################Task A#####################################

#  Creating the funtion funtioncheckresult() for getting the results according
#  to the condition i.e. if the quota limit is greater than 90 then perform certain
#  action. In this function i am checking the Disk weightage using if statement.


functioncheckresult()	#function for checking the disk space
{
	if [ $result  -gt  $LIMIT ];then
		
		echo ""
		echo "#######################Task A##########################"
		echo ""
		echo "You are currently using $result% of your quota."
		echo "Consider reducing your usage."
		echo ""
		echo "#######################################################"

	elif [ $result -le $LIMIT ];then
		
		echo ""
		echo "#########################Task A########################"
		echo ""
		echo "You are currently using $result% of your quota."
		echo ""
		echo "#######################################################"
	fi
	
	return 0
}


#############################################################################

#################################Task B######################################

#  Creating the funtion functionlargestdirectory () to get the list of 10 biggest directories 
#  in your home directory. In this funtion we have to assume that the 
#  result is in kBytes.

functionlargestdirectories()
{
	D_LIMIT=10
	Homedir=` basename $user/*/`

	echo ""
	echo "###################################Task B#######################################"
	echo ""
	echo "The 10 biggest directories in your home directory are: (in kBytes)"
	echo "Size   Directory"
	echo "----   ---------"
	echo ""

	du -k $Homedir | head -$D_LIMIT		# du -k: Write the files sizes
						# in  units  of  1024  bytes,
						# rather than the default
						# 512-byte units.
	return 0
}

############################################################################

###################################Task C###################################

#  Creating the funtion functionlargestdirectory () to get the list of 10 
#  biggest directories in your home directory. In this funtion we do 
#  not have to assume that the result is in kBytes instead it shows the 
#  size of the Directories in a user frienly manner.


functionDirectores()
{
	D_LIMIT=10
        Homedir=` basename $user/*/`

        echo ""
	echo "###################################Task C#######################################"
        echo ""
        echo "The 10 biggest directories in your home directory are: (User Friendly manner)"
        echo "Size   Directory"
        echo "----   ---------"
        echo ""

        du -h $Homedir | head -$D_LIMIT		# du -h: All sizes are scaled
						# to a human  readable  format,
				                # for  example,  14K, 234M, 
						# 2.7G, or 3.0T. Scaling is
						# done by repetitively dividing
						# by 1024.
	echo ""
	return 0
}


############################################################################################

#  These are used to run the Functions: functionquota(), functioncheckresult(), functionlargestDirectory()
#  and functionDirectores()

functionquota 			#functionquota()
functioncheckresult		#functioncheckresult()
functionlargestdirectories	#functionlargestDirectory()
functionDirectores		#functionDirectores()


############################################################################################

################################   README     ##############################################
#
#
#Name      : Piyush Garg
#Student ID: s3218012
#
#The script includes all the 3 tasks are it was required.
#
#
#Task A:
#To check the disk sapace.
#
#
#Task B:
#To produce the output of 10 biggest directories in kBytes. In this we have to a$
#
#
#Task C:
#To produce the output of 10 biggest directories in a User Friendly format i.e. $
#
#
#Refrences:
#
#1. http://www.cyberciti.biz/tips/linux-unix-du-command-examples.html
#
#2. UNIX in a nutshell / Arnold Robbins.
#3. Mastering in Unix
#4. Unix Survival Skills (School of Computer Science)
#
